package com.wt.admin.config;

import com.wt.admin.config.cache.Cache;
import com.wt.admin.config.cache.CacheManager;
import com.wt.admin.config.cache.impl.ChatClientCache;
import com.wt.admin.config.cache.impl.ChatContentCache;
import com.wt.admin.domain.vo.chart.ChatModelContentVO;
import com.wt.admin.domain.vo.sys.UserVO;
import com.wt.admin.service.vector.Vector;
import com.wt.admin.service.vector.impl.ESVectorImpl;
import com.wt.admin.service.vector.impl.MemoryVectorImpl;
import lombok.Data;
import org.dromara.easyai.entity.KeyWordForSentence;
import org.dromara.easyai.naturalLanguage.TalkToTalk;
import org.dromara.easyai.naturalLanguage.languageCreator.CatchKeyWord;
import org.dromara.easyai.naturalLanguage.word.MyKeyWord;
import org.dromara.easyai.naturalLanguage.word.WordEmbedding;
import org.dromara.easyai.rnnJumpNerveCenter.RRNerveManager;
import org.dromara.easyai.yolo.FastYolo;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.memory.InMemoryChatMemory;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.embedding.TokenCountBatchingStrategy;
import org.springframework.ai.vectorstore.SimpleVectorStore;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;

@Configuration
public class GlobalBeanConfig {

    @Bean("messageSource")
    public MessageSource messageSource() {
        ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasename("classpath:i18n/msg");
        messageSource.setDefaultEncoding("UTF-8");
        return messageSource;
    }

    @Bean
    public Cache<Long, UserVO> userCache(){
        return CacheManager.getCache("user");
    }

    @Bean("newAsyncExecutor")
    public Executor newAsyncExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(50);
        taskExecutor.setMaxPoolSize(100);
        taskExecutor.setQueueCapacity(50);
        taskExecutor.setKeepAliveSeconds(100);
        taskExecutor.setThreadNamePrefix("定时任务-");
        taskExecutor.initialize();
        return taskExecutor;
    }

    @Bean("publicThread")
    public Executor publicThread() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setCorePoolSize(50);
        taskExecutor.setMaxPoolSize(100);
        taskExecutor.setQueueCapacity(50);
        taskExecutor.setKeepAliveSeconds(100);
        taskExecutor.setThreadNamePrefix("通用任务-");
        taskExecutor.initialize();
        return taskExecutor;
    }

    /**
     * 词向量嵌入器,语义神经
     * @return
     */
    @Bean("wordEmbedding")
    public Cache<String,WordAndRRManager> wordEmbedding(){
        return CacheManager.getCache("wordEmbedding");
    }

    @Bean("myKeyWord")
    public Cache<Integer,MyKeyWord> myKeyWordMap(){
        return CacheManager.getCache("myKeyWord");
    }

    @Bean("catchKeyWord")
    public Cache<Integer, CatchKeyWord> catchKeyWordMap(){
        return CacheManager.getCache("catchKeyWord");
    }

    @Bean("sensorKeyWordMapper")
    public Cache<Integer,Cache<Integer,List<KeyWordForSentence>>> sensorKeyWordMapper(){
        return CacheManager.getCache("sensorKeyWordMapper");
    }

    @Bean("chatContents")
    public ChatContentCache<Integer, ChatModelContentVO> chatContents(){
        return new ChatContentCache<>();
    }

    @Bean("chatClients")
    public ChatClientCache<Integer, ChatClient> chatClients(){
        return new ChatClientCache<>();
    }

    @Bean
    public InMemoryChatMemory chatMemory(){
        return new InMemoryChatMemory();
    }

    @Bean
    ChatClient chatClient(ChatClient.Builder builder) {
        return builder.defaultSystem("你的名字是{name},{desc};结合向量库中你作为{name}的对话，回答这个问题。")
                .build();
    }

    @Bean
    public VectorStore memoryVectorStore(EmbeddingModel embeddingModel) {
        return SimpleVectorStore.builder(embeddingModel)
                .batchingStrategy(new TokenCountBatchingStrategy())
                .build();
    }

    @Bean
    @ConditionalOnProperty(name = "spring.vector.es", havingValue = "true")
    public Vector es() {
        return new ESVectorImpl();
    }

    @Bean
    @ConditionalOnProperty(name = "spring.vector.es", havingValue = "false")
    public Vector memory() {
        return new MemoryVectorImpl();
    }

    @Bean("imageYoloManager>")
    public Cache<Integer,FastYolo> imageYoloManager(){
        return CacheManager.getCache("imageYoloManager");
    }

    @Bean("keyWordValueList")
    public List<KeywordValue> keyWordValueList(){
        return new ArrayList<>();
    }

    @Data
    public static class KeywordValue{
        private String keywordValue;
        private Integer typeId;
        private Long index;//该关键词索引id
        public KeywordValue(String keywordValue, Integer typeId, Long index) {
            this.keywordValue = keywordValue;
            this.typeId = typeId;
            this.index = index;
        }
        public KeywordValue(String keywordValue, Integer typeId) {
            this.keywordValue = keywordValue;
            this.typeId = typeId;
        }
    }

    @Data
    public static class WordAndRRManager {

        private WordEmbedding wordEmbedding;
        private RRNerveManager rrNerveManager;
        private TalkToTalk talkToTalk;

        public WordAndRRManager() {
            this.wordEmbedding = new WordEmbedding();
            this.rrNerveManager = new RRNerveManager(wordEmbedding);
        }

    }


}
